/*
 * @lc app=leetcode.cn id=897 lang=typescript
 *
 * [897] 递增顺序搜索树
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */

function increasingBST(root: TreeNode | null): TreeNode | null {
    if (root === null) return null;
    const tra = (root: TreeNode | null): TreeNode[] => {
        if (root === null) return [];
        return [...tra(root.left), root, ...tra(root.right)];
    };

    const [head, ...body] = tra(root);
    let now = head;
    body.forEach((i) => {
        now.left = null;
        now.right = i;
        now = i;
    });
    now.right = null;
    now.left = null;
    return head;
}
// @lc code=end
